import { Controller } from "../../mvc/core/Controller";
import { IQuestionController } from "./interfaces/IQuestionController";
import { IView } from "../../mvc/interfaces/IView";
import { QuestionModule } from "../../../modules/QuestionModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { QuestionDataTool } from "../view/question/QuestionDataTool";
import { SQuestionPrepareStart } from "../../../Message/Public/SQuestionPrepareStart";
import { ClockManager } from "../manager/ClockManager";
import { DataEvent } from "../events/DataEvent";
import { SQuestionData } from "../../../Message/Public/SQuestionData";
import { SSeqQuestionMark } from "../../../Message/Public/SSeqQuestionMark";
import { SUpdatePlayerMark } from "../../../Message/Public/SUpdatePlayerMark";
import { SQuestionPlayerInfo } from "../../../Message/Public/SQuestionPlayerInfo";
import { SQuestionReward } from "../../../Message/Public/SQuestionReward";
import { SReward } from "../../../Message/Public/SReward";
import { EReward } from "../../../Message/Public/EReward";
import { QuestionRewardWindow } from "../view/question/QuestionRewardWindow";
import { GameProxy } from "../mvc/GameProxy";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { IQuestionModule } from "../../../modules/interfaces/IQuestionModule";
//class QuestionController
    
    export  class QuestionController extends Controller implements IQuestionController
    {
       

        protected /* override */ initView(): IView
        {
            if (this._question == null) 
            {
                this._question = new QuestionModule();
            }
            return this._question;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicQuestionPrepareStart, this.onQuestionPrepareStartHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicQuestionData, this.onQuestionDataHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicQuestionEnd, this.onQuestionEndHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicQuestionUpdateRank, this.onQuestionUpdateRankHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicQuestionCanAnswer, this.onQuestionCanAnswerHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicQuestionPlayerInfo, this.onQuestionPlayerInfoHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicQuestionReward, this.onQuestionRewardHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicQuestionPublicAnswer, this.onQuestionPublicAnswerHandler);
            Dispatcher.addEventListener(EventName.AnswerQuestion, this.onAnswerQuestionHandler);
            Dispatcher.addEventListener(EventName.QuestionBtnClick, this.onQuestionBtnClickHandler);
            Dispatcher.addEventListener(EventName.QuestionWindowOpen, this.onQuestionWindowOpenHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdataHandler);
            return;
        }

        /* internal  */onQuestionPrepareStartHandler(arg1: MessageBlock): void
        {
            var loc1=null;
            if (cache.role.entityInfo.level < 30) 
            {
                return;
            }
            QuestionDataTool.functCode = -1;
            loc1 = arg1.messageBase as SQuestionPrepareStart;
            view.show();
            QuestionDataTool.sQuestionPrepareStart = loc1;
            QuestionDataTool.isStart = true;
            var loc2=ClockManager.instance.nowDate;
            var loc3=loc1.startDt;
            var loc4=Math.abs((loc3.time - loc2.time) / 1000);
            QuestionDataTool.setTimer(loc4);
            if (this._question != null) 
            {
                this._question.updateQuestion(loc1);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.QuestionShowMainBtn));
            return;
        }

        /* internal  */onQuestionDataHandler(arg1: MessageBlock): void
        {
            if (cache.role.entityInfo.level < 30) 
            {
                return;
            }
            QuestionDataTool.functCode = -1;
            var loc1=arg1.messageBase as SQuestionData;
            QuestionDataTool.sQuestionData = loc1;
            QuestionDataTool.answerState = 0;
            QuestionDataTool.isFuncUse = false;
            QuestionDataTool.haveAnswer = false;
            QuestionDataTool.setTimer(10);
            if (this._question != null) 
            {
                this._question.updateQuestion(null, loc1);
            }
            if (view.isHide && !QuestionDataTool.isStart) 
            {
                view.show();
                QuestionDataTool.isStart = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.QuestionShowMainBtn));
            }
            return;
        }

        /* internal  */onQuestionEndHandler(arg1: MessageBlock): void
        {
            QuestionDataTool.functCode = -1;
            QuestionDataTool.sQuestionPrepareStart = null;
            QuestionDataTool.sQuestionData = null;
            QuestionDataTool.answerState = 0;
            QuestionDataTool.isStart = false;
            if (this._question != null) 
            {
                this._question.updateQuestion(null, null);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.QuestionRemoveMainBtn));
            return;
        }

        /* internal  */onQuestionUpdateRankHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SSeqQuestionMark;
            QuestionDataTool.questionMark = loc1;
            if (!(this._question == null) && !this._question.isHide && !(loc1 == null)) 
            {
                this._question.updateRank(loc1.questionMarks);
            }
            return;
        }

        /* internal  */onQuestionUpdatePlayerMarkHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SUpdatePlayerMark;
            return;
        }

        /* internal  */onQuestionCanAnswerHandler(arg1: MessageBlock): void
        {
            QuestionDataTool.answerState = 1;
            QuestionDataTool.setTimer(10);
            if (this._question != null) 
            {
                this._question.canAnswer();
            }
            return;
        }

        /* internal  */onQuestionPlayerInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SQuestionPlayerInfo;
            QuestionDataTool.sQuestionPlayerInfo = loc1;
            if (this._question != null) 
            {
                this._question.updatePlayerInfo(loc1);
            }
            return;
        }

        /* internal  */onQuestionRewardHandler(arg1: MessageBlock): void
        {
            var loc3=null;
            var loc4=0;
            var loc5=0;
            var loc6=0;
            var loc9=null;
            if (cache.role.entityInfo.level < 30) 
            {
                return;
            }
            var loc1=arg1.messageBase as SQuestionReward;
            var loc2=loc1.rewards;
            var loc7=[];
            var loc8=0;
            while (loc8 < loc2.length) 
            {
                if (loc2[loc8] == null) 
                {
                    return;
                }
                if ((loc3 = loc2[loc8] as SReward).type != EReward._ERewardExperience) 
                {
                    if (loc3.type == EReward._ERewardItem) 
                    {
                        loc5 = loc3.code;
                        loc6 = loc3.num;
                        loc7.push({"code": loc5, "itemNum": loc6});
                    }
                }
                else 
                {
                    loc4 = loc4 + loc3.num;
                }
                ++loc8;
            }
            if (loc4 > 0 || loc5 > 0) 
            {
                (loc9 = new QuestionRewardWindow()).updateExpData(loc4, loc7);
                loc9.show();
            }
            return;
        }

        /* internal  */onQuestionPublicAnswerHandler(arg1: MessageBlock): void
        {
            QuestionDataTool.answerState = 2;
            QuestionDataTool.setTimer(3);
            if (!(this._question == null) && !this._question.isHide) 
            {
                this._question.publicAnswer();
            }
            return;
        }

        /* internal  */onAnswerQuestionHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            GameProxy.roleProxy.answerQuestion(loc1.num, loc1.questionId, loc1.answer, loc1.funcCode);
            return;
        }

        /* internal  */onQuestionBtnClickHandler(arg1: DataEvent): void
        {
            if (view.isHide) 
            {
                view.show();
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */onQuestionWindowOpenHandler(arg1: DataEvent): void
        {
            if (cache.role.entityInfo.level < 30) 
            {
                MsgManager.showRollTipsMsg(Language.getString(35600));
                return;
            }
            view.show();
            return;
        }

        /* internal  */onSceneUpdataHandler(arg1: DataEvent): void
        {
            if (QuestionDataTool.isStart && !cache.copy.isInCopy) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.QuestionShowMainBtn));
            }
            return;
        }

        private /* var */_question: IQuestionModule;
    }
